﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TimeTrak.Views;
using TimeTrak.BLL;

namespace TimeTrak.Controllers
{
    public class TimeEditController
    {
        public TimeEditForm View { get; private set; }
        public Task CurrentTask { get; set; }

        public TimeEditController()
        {
            View = new TimeEditForm();

            View.AddTimeButton.Click += new EventHandler(AddTimeButton_Click);
            View.RemoveTimeButton.Click += new EventHandler(RemoveTimeButton_Click);

        }

        void RemoveTimeButton_Click(object sender, EventArgs e)
        {
            TimeUnitType type = (TimeUnitType)View.UnitsComboBox.SelectedValue;
            decimal amount = decimal.Parse(View.Amount.Text) * -1.0m;
            DateTime editDate = View.DateTimePicker1.Value;

            CurrentTask.ManualAddTime(editDate, type, amount);
            View.Hide();
        }

        void AddTimeButton_Click(object sender, EventArgs e)
        {
            TimeUnitType type = (TimeUnitType)View.UnitsComboBox.SelectedValue;
            decimal amount = decimal.Parse(View.Amount.Text);
            DateTime editDate = View.DateTimePicker1.Value;

            CurrentTask.ManualAddTime(editDate, type, amount);
            View.Hide();
        }

        public void Show(Task task)
        {
            CurrentTask = task;
            LoadView();
            View.ShowDialog();
        }

        private void LoadView()
        {
            View.DateTimePicker1.Text = DateTime.Today.ToShortDateString();
            View.UnitsComboBox.DataSource = Enum.GetValues(typeof(TimeUnitType));
            View.UnitsComboBox.SelectedItem = TimeUnitType.Hour; //set default

        }
    }
}
